package com.CmJava.crowd.controller;


import com.CmJava.crowd.entity.ResultEntity;
import com.CmJava.crowd.entity.User;
import com.CmJava.crowd.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;


    private int pageNum=8;

    //获取用户列表
    @RequestMapping("userList")
    @ResponseBody
    public PageInfo userList(@RequestParam(value = "currentPage" ) String currentPage,
                              @RequestParam(value = "keyWord") String keyWord){
        int cp = Integer.parseInt(currentPage);
        PageInfo<User> userPageInfo= userService.userList(cp,pageNum,keyWord);
        return userPageInfo;
    }

    //获取特定用户信息
    @RequestMapping("getUserDetail")
    @ResponseBody
    public ResultEntity userDetail(@RequestParam(value = "primaryKey" ) String primaryKey){
        ResultEntity resultEntity= userService.userDetail(primaryKey);
        return resultEntity;
    }

    //获取特定用户的项目信息
    @RequestMapping("getUserGoods")
    @ResponseBody
    public ResultEntity getUserGoods(@RequestParam(value = "primaryKey" ) String primaryKey){
        ResultEntity resultEntity= userService.getUserGoods(primaryKey);
        return resultEntity;
    }

    //获取特定用户的项目最近获筹信息
    @RequestMapping("getTheseGoodRedisInfo")
    @ResponseBody
    public ResultEntity getTheseGoodRedisInfo(@RequestParam(value = "goodId" ) String goodId){
        List<String> goodIds = Arrays.asList(goodId.split(","));
        ResultEntity resultEntity = userService.getTheseGoodRedisInfo(goodIds);
        return resultEntity;
    }

    //删除用户
    @RequestMapping("deleteUser")
    @ResponseBody
    public ResultEntity deleteUser(@RequestParam(value = "primaryKey" ) String primaryKey){
        ResultEntity resultEntity = userService.deleteUser(primaryKey);
        return resultEntity;
    }

    //修改用户
    @RequestMapping("updateThisUser")
    @ResponseBody
    public ResultEntity updateThisUser(@RequestParam(value = "primaryKey" ) String primaryKey,
                                       @RequestParam(value = "userAccount" ) String userAccount,
                                       @RequestParam(value = "userPassword" ) String userPassword,
                                       @RequestParam(value = "userIdNumber" ) String userIdNumber,
                                       @RequestParam(value = "userName" ) String userName){
        User user = new User(Integer.parseInt(primaryKey),userAccount,userPassword,userName,userIdNumber,null,null);
        ResultEntity resultEntity = userService.updateThisUser(user);
        return resultEntity;
    }

    //审核用户
    @RequestMapping("examineThisUser")
    @ResponseBody
    public ResultEntity examineThisUser(@RequestParam(value = "primaryKey" ) String primaryKey,
                                       @RequestParam(value = "userIdNumber" ) String userIdNumber,
                                       @RequestParam(value = "userName" ) String userName){
        User user = new User(Integer.parseInt(primaryKey),null,null,userName,userIdNumber,null,1);
        ResultEntity resultEntity = userService.examineThisUser(user);
        return resultEntity;
    }


}
